package com.zhoulu.controller.book;

import com.zhoulu.domain.book.BookBean;
import com.zhoulu.domain.util.LayuiResult;
import com.zhoulu.service.book.BookService;
import com.zhoulu.util.PageRequest;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;


@RequestMapping("/book")
@RestController
public class BookController {

    @Autowired
    private BookService bookService;

    /**
     * 查询图书信息集合
     * @param bookBean
     * @return
     */
    @RequestMapping("findBookList")
    public LayuiResult findBookList(BookBean bookBean, PageRequest pageRequest){
        try{
            PageInfo<BookBean> pageInfo = bookService.findBookListPage(bookBean, pageRequest);
            return LayuiResult.success("查询成功",pageInfo.getTotal(), pageInfo.getList());
        }catch(Exception error){
            error.printStackTrace();
            return LayuiResult.error(error.getMessage());
        }
    }

    /**
     * 新增图书
     * @param bookBean
     */
    @RequiresPermissions("book:add")
    @PostMapping(value = "insertBook")
    public LayuiResult insertBook(BookBean bookBean){
        try{
            Preconditions.checkNotNull(bookBean, "图书信息为空，无法新增");
            Preconditions.checkArgument(!Strings.isNullOrEmpty(bookBean.getBookNo()), "图书编号为空，不允许新增");
            Preconditions.checkArgument(!Strings.isNullOrEmpty(bookBean.getBookName()), "图书名称为空，不允许新增");
            Preconditions.checkArgument(!Strings.isNullOrEmpty(bookBean.getPublish()), "图书出版社为空，不允许新增");
            BookBean bookBeanQueryParam = new BookBean();
            bookBeanQueryParam.setBookNo(bookBean.getBookNo());
            List<BookBean> resultList = bookService.findBookList(bookBeanQueryParam);
            Preconditions.checkArgument((resultList == null || resultList.size() == 0), "编号已存在，不允许重复新增");
            return bookService.insertBook(bookBean);
        }catch(Exception error){
            error.printStackTrace();
            return LayuiResult.error(error.getMessage());
        }
    }

    /**
     * 修改图书信息
     * @param bookBean
     */
    @RequiresPermissions("book:update")
    @PostMapping("updateBook")
    public LayuiResult updateBook(BookBean bookBean){
        try{
            Preconditions.checkNotNull(bookBean, "图书信息为空，无法编辑");
            Preconditions.checkArgument(!Strings.isNullOrEmpty(bookBean.getBookNo()), "图书编号为空，不允许修改");
            Preconditions.checkArgument(!Strings.isNullOrEmpty(bookBean.getBookName()), "图书名称为空，不允许修改");
            Preconditions.checkArgument(!Strings.isNullOrEmpty(bookBean.getPublish()), "图书出版社为空，不允许修改");
            bookService.updateBook(bookBean);
            return LayuiResult.success("修改图书成功");
        }catch(Exception error){
            error.printStackTrace();
            return LayuiResult.error(error.getMessage());
        }
    }

    /**
     * 删除图书
     * @param bookNo 图书编号
     */
    @RequiresPermissions("book:deleteBookByNo")
    @RequestMapping("deleteBookByNo")
    public LayuiResult deleteBookByNo(String bookNo){
        try{
            Preconditions.checkArgument(!Strings.isNullOrEmpty(bookNo), "图书编号为空，不允许删除");
            bookService.deleteBookByNo(bookNo);
            return LayuiResult.success("删除图书成功");
        }catch(Exception error){
            error.printStackTrace();
            return LayuiResult.error(error.getMessage());
        }
    }
}
